<template>
  <div class="liveCodeData">
    <div class="formGroup">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px" class="">
        <el-form-item label="选择时间：" prop="userName2">
          <div class="time-big">
            <ul class="timeType">
              <li :class="selectedRange=='today'?`active`:``" @click="getTime('today')">今天</li>
              <li :class="selectedRange=='last7Days'?`active`:``" @click="getTime('last7Days')">近1周</li>
              <li :class="selectedRange=='last15Days'?`active`:``" @click="getTime('last15Days')">近2周</li>
              <li :class="selectedRange=='last30Days'?`active`:``" @click="getTime('last30Days')">近1月</li>
            </ul>
            <el-date-picker
              size="small"
              v-model="timeRange"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </div>

        </el-form-item>
        <el-form-item>
          <div class="search">
            <el-input v-model="queryParams.userName1" placeholder="搜索渠道名称" clearable/>
            <el-button icon="el-icon-search" size="small" @click="handleQuery"></el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="content-big">
    <div class="trendAnalysis">
      <div class="center-title">新增好友走势</div>
      <lineEchart :id="`trendAnalysisContent`"  :lineXDataList="lineXDataList" :lineYDataList="lineYDataList"></lineEchart>
    </div>
      <div class="content-right">
        <div class="center-title">各渠道新增好友数<span></span></div>
        <div class="addBtn" @click="getChooseChannels"><i class="el-icon-plus"></i>选择渠道进行分析（2/10）</div>
        <ul class="dataList">
          <li>开心<span>0</span></li>
          <li>不开心<span>0</span></li>
        </ul>
      </div>
    </div>
    <div class="tableAnalysis">
      <div class="center-title">数据报表</div>
      <el-button plain class="exportData">导出数据</el-button>
      <el-table
        :data="tableDataList"
        ref="customerMgtList"
      >
        <el-table-column
          :label="item.label"
          align="center"
          v-for="(item, index) in tableColumns"
          :key="index"
          :prop="item.key"
          :min-width="item.minWidth ? item.minWidth : ``"
          :fixed="item.fixed ? item.fixed : false"
          :show-overflow-tooltip="true"
        >
          <!--用于表格自定义-->
          <template slot-scope="scope">
            <div>{{ scope.row[item.key] }}</div>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
<!--    没有数据用下面的-->
    <!--    <div class="empty-qrcodes" v-if="actived === 1">-->
    <!--      <img class="empty-cover" src="https://s.yezgea02.com/zeus/images/empty@2x.png" />-->
    <!--      <div class="empty-description">-->
    <!--        <span style="display: block; margin-bottom: 8px; margin-top: 24px; font-size: 18px;">-->
    <!--          <strong>您还没有创建渠道活码</strong>-->
    <!--        </span>-->
    <!--        <span style="display: block; margin-bottom: 24px;color: rgba(0,0,0,.45);">您需要先创建渠道活码才能获取数据，请点击下方按钮</span>-->
    <!--        <div class="empty-foooter">-->
    <!--          <el-button size="small" type="primary">立即创建</el-button>-->
    <!--        </div>-->
    <!--      </div>-->
    <!--    </div>-->
    <chooseChannels v-if="showChooseChannels" :visableIf.sync="showChooseChannels"></chooseChannels>
  </div>
</template>
<script>
import {listUser} from "@/api/system/user";
import {getTime} from "@/utils";

export default {
  name: "liveCodeData",
  components: {
    'lineEchart': () => import('./lineEchart.vue'),//导出弹窗
    'chooseChannels': () => import('./chooseChannels.vue'),//导出弹窗
  },
  data() {
    return {
      showChooseChannels:false,
      queryParams: {},
      timeRange: [],
      dataList:[
        {value:1000,name:'扫一扫',},
        {value:1200,name:'手机号',},
        {value:1200,name:'名片',},
        {value:600,name:'群聊',},
        {value:800,name:'微信好友',},
        {value:900,name:'第三方专属客户',},
        {value:100,name:'其他',},
      ],
      sexList:[
        {value:1000,name:'男',},
        {value:1200,name:'女',},
      ],
      lineXDataList:['2023-01', '2023-02', '2023-03', '2023-04'],
      lineYDataList:[{
        name: 'Email',
        type: 'line',
        stack: 'Total',
        data: [120, 132, 101, 134]
      },
        {
          name: 'Union Ads',
          type: 'line',
          stack: 'Total',
          data: [220, 182, 191, 234,]
        },
        {
          name: 'Video Ads',
          type: 'line',
          stack: 'Total',
          data: [150, 232, 201, 154]
        },
        {
          name: 'Direct',
          type: 'line',
          stack: 'Total',
          data: [320, 332, 301, 334]
        },
        {
          name: 'Search Engine',
          type: 'line',
          stack: 'Total',
          data: [820, 932, 901, 934]
        }],
      userList: [
        {
          value: '1',
          label: '员工1'
        }, {
          value: '2',
          label: '员工2'
        }
      ],
      selectedRange: 'today',
      tableHeight:600,
      tableColumns: [
        {key: 'name', label: `渠道名称`, visible: true, minWidth: 150, fixed: true},
        {key: 'mobile', label: `日期`, visible: true, minWidth: 150, fixed: false},
        {key: 'email', label: `新增好友数`, visible: true, minWidth: 200},
        {key: 'last_login_time', label: `留存好友数`, visible: true, minWidth: 200},
        {key: 'create_time', label: `活跃客户数`, visible: true, minWidth: 200},
        {key: 'check_payCreate_time', label: `累计流失客户数`, visible: true, minWidth: 200},
        {key: 'check_payStatus_name', label: `客户总数`, visible: true, minWidth: 150},
      ],
      tableDataList: [],
      total: 8, // 表格的总条数
    }
  },
  watch: {},
  computed: {
    dateRange() {
      const now = new Date();
      switch (this.selectedRange) {
        case 'yesterday':
          return [
            new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1),
            new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1),
          ];
        case 'today':
          return [
            now,
            new Date(now.getFullYear(), now.getMonth(), now.getDate()),
          ];
        case 'last7Days':
          return [
            new Date(now.getTime() - (6 * 24 * 60 * 60 * 1000)),
            now,
          ];
        case 'last15Days':
          return [
            new Date(now.getTime() - (14 * 24 * 60 * 60 * 1000)),
            now,
          ];
        case 'last30Days':
          return [
            new Date(now.getTime() - (29 * 24 * 60 * 60 * 1000)),
            now,
          ];
        default:
          return [];
      }
    },
  },
  created() {
    //this.getList();

  },
  methods: {
    getTime(type) {
      this.selectedRange = type;
      setTimeout(() => {
        console.log('时间范围', this.dateRange);
        this.timeRange = [];
        this.timeRange = [this.fiterTime(this.dateRange[0]), this.fiterTime(this.dateRange[1])];
        console.log('转化时间', this.timeRange);
      }, 0)
    },
    fiterTime(value) {
      if (!value) return '';
      const date = new Date(value);
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, '0');
      const day = date.getDate().toString().padStart(2, '0');
      return `${year}-${month}-${day}`;
    },
    getChooseChannels(){
      this.showChooseChannels=true;
    },
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.tableDataList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
}
</script>


<style scoped lang="scss">
.liveCodeData {
  background: #fff;
  margin: 10px;
}
.timeType {
  display: flex;
  align-items: center;
  margin-right: 10px;

  li {
    border: 1px solid #eee;
    height: 32px;
    margin: 0;
    padding: 0 15px;
    list-style: none;
    cursor: pointer;

    &.active {
      border-color: #2d8cf0;
      color: #2d8cf0;
    }
  }
}

.time-big {
  display: flex;
  align-items: center;
}
.formGroup {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  width: 100%;
  padding-top: 10px;
  border-bottom: 1px solid #eee;
}
.content-center {
  display: flex;
  height: 300px;
  .center-left {
    width: 50%;
    height: 100%;
  }
  .center-right {
    width: 50%;
    height: 100%;
  }
}
.center-title {
  height: 30px;
  display: flex;
  align-items: center;
  font-weight: bold;
  margin-bottom: 10px;
}
.trendAnalysis {
  width: 100%;
  flex: 1;
  border-right: 1px solid #eee;
  padding: 20px;

}
.tableAnalysis {
  padding: 20px;
  position: relative;
  .center-title {
    margin-bottom: 20px;
    font-weight: bold;
  }
  .exportData {
    position: absolute;
    right: 20px;
    top: 20px;

  }
}
.search {
  display: flex;

  ::v-deep.el-input__inner {
    border-right: none !important;
    border-radius: 4px 0px 0px 0px;
  }

  ::v-deep .el-button--default {
    border-radius: 0px 4px 4px 0px;
    padding: 0px 10px;
  }
}
.content-big {
  display: flex;
  border-bottom: 1px solid #eee;
  .content-right {
    width: 320px;
    padding: 20px;
  }
}
.addBtn {
  padding: 0px 10px;
  height: 46px;
  border: 1px dashed #eee;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.dataList {
  li {
    list-style: none;
    position: relative;
    padding: 12px 20px 12px 20px;
    border: 1px dashed #e0e0e0;
    border-left: 2px solid #e0e0e0;
    margin-top: 8px;
    display: flex;
    justify-content: space-between;
    span {
      color: #C03639;
    }
  }
}
</style>
